package com.example.scaffolding.util

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.After
import org.junit.Before
import org.junit.Test

import org.junit.Assert.*
import org.junit.Rule
import org.junit.runner.RunWith
import java.io.File
import java.io.OutputStream
import java.util.concurrent.ExecutionException


@RunWith(AndroidJUnit4::class)
class ImageLoaderTest2 {
    private lateinit var context: Context

    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext()
    }

    @After
    fun tearDown() {
    }

    @Test(expected = ExecutionException::class)
    fun load_InvalidUrlShouldException() {
        val filePath = context.filesDir.toString() + "/test1.jpg"
        ImageLoader.load(context, filePath)
    }

    @Test
    fun load_bmpHeightWidthShouldNotBeZero() {
        val filePath = context.filesDir.toString() + "/test2.jpg"
        val bmp = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888)
        try {
            val file = File(filePath)

            val fos: OutputStream = file.outputStream()
            bmp.compress(Bitmap.CompressFormat.JPEG, 80, fos)
            fos.flush()
            fos.close()
            val ss = file.length()
            Log.i("ImageLoaderTest","source image file size: $ss")
        } catch (e: Exception) {
            e.printStackTrace()
            assertTrue("source image file should be ok", false)
        }

        val targetPath = ImageLoader.load(context, filePath)
        Log.i("ImageLoaderTest","target image file: $targetPath")
        assertTrue(File(targetPath).exists())
    }
}